
#import "LoginViewController.h"
#import "PrivateViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UILabel *theme;
@property (strong, nonatomic) IBOutlet BaseTextField *input1;
@property (strong, nonatomic) IBOutlet BaseTextField *input2;
@property (strong, nonatomic) IBOutlet UIButton *selectBtn;
@property (strong, nonatomic) IBOutlet UIButton *registerBtn;
@property (strong, nonatomic) IBOutlet UIButton *btn;
@property (strong, nonatomic) IBOutlet UIView *toolView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *textviewW;
@end

@implementation LoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)initUI{
    self.theme.text = self.isLogin ? LS(@"欢迎登录") : LS(@"创建账户");
    self.registerBtn.hidden = !self.isLogin;
    self.toolView.hidden = !self.isLogin;
    [self.btn setTitle:(self.isLogin ? LS(@"登录") : LS(@"注册")) forState:UIControlStateNormal];

}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 204) {
        [self backBtnAction];
    }else if (sender.tag == 203){
        sender.selected = !sender.selected;
    }
    if ([sender.currentTitle isEqualToString:LS(@"注册账号")]) {
        LoginViewController *vc = [LoginViewController new];
        vc.isLogin = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([sender.currentTitle isEqualToString:LS(@"登录")]){
        if ([self judgeInput]) {
            [UserTool login:self.input1.text psw:self.input2.text complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    APP.user = dict[@"data"];
                    [self backBtnAction];
                }else{
                    [Toast msg:LS(dict[@"msg"])];
                }
            }];
        }
    }else if ([sender.currentTitle isEqualToString:LS(@"注册")]){
        if ([self judgeInput]){
            [UserTool register:self.input1.text psw:self.input2.text complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    LoginViewController *vc   = self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2];
                    vc.input1.text = self.input1.text;
                    vc.input2.text = self.input2.text;
                    [self backBtnAction];
                }else{
                    [Toast msg:LS(dict[@"msg"])];
                }
            }];
        }
    }else if ([sender.currentTitle isEqualToString:LS(@"《用户协议》")]){
        PrivateViewController *vc  = [PrivateViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([sender.currentTitle isEqualToString:LS(@"《隐私条款》")]){
        PrivateViewController *vc  = [PrivateViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (BOOL)judgeInput{
    if (self.input1.text.length <= 0){
        [Toast msg:LS(@"账号不能为空")];
        return NO;
    }else if (self.input2.text.length <= 0){
        [Toast msg:LS(@"密码不能为空")];
        return NO;
    }else if ([self.input1.text isNumberOrAlphabet] == NO){
        [Toast msg:LS(@"账号只能包含数字和字母")];
        return NO;
    }else if ([self.input2.text isNumberOrAlphabet] == NO){
        [Toast msg:LS(@"密码只能包含数字和字母")];
        return NO;
    }else if (self.isLogin == YES && self.selectBtn.selected == NO){
        [Toast msg:LS(@"请先阅读协议和隐私条款")];
        return NO;
    }
    else{
        return YES;
    }
}
#pragma mark - UITableViewDataSource
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    if ([URL.absoluteString isEqualToString:@"user"]) {
        return NO;
    }
    if ([URL.absoluteString isEqualToString:@"privacy"]){
        return NO;
    }
    return YES;
}

@end
